<?php
namespace Wap\Service;

class PraiseService
{
    /**
     * @method is_good
     *
     */

    protected static $tableName = "praise";

    private static $prefix = "praise_";
    private static $expire = 7200;//30s

    public static function setPraise($key, $value)
    {
        $key = self::$prefix . $key;
        S($key, $value, self::$expire);//30s
    }

    public static function getPraise($key)
    {
//        \Think\Log::record('keyis' . $key);
        // $key = self::$prefix.$key;
        // \Think\Log::record('kkk'.$key);
        // return S( $key );

        $row = self::$prefix . $key;
//        trace("key:",$key,"SQL");
        /*        if (!$row) {
                    \Think\Log::record('row is true');*/
        $condition['id'] = $key;
        $row = D(self::$tableName)->where($condition)->cache('wap_praise' . $key, 60)->find();
//        trace($row, "row:" . $condition['id'], "SQL");
        return $row;

        /*        }
                return S($row);*/
    }

    public static function is_good($id = -1, $time = 0)
    {
        $result = array("good" => false, "good_startdate" => false, "good_enddate" => false, "id" => $id, "time" => $time, "startdate" => 0, "enddate" => 0, 'msg' => "");
        if (!$time || !$id) {
            $result['id'] = $id;
            $result['time'] = $time;
            $result['msg'] = "活动信息错误";
            return $result;
        }

        self::setPraise($id, null);
        $condition['id'] = $id;
        $row = self::getPraise($condition['id']);
        if (!$row) {
            $row = D(self::$tableName)->where($condition)->cache('wap_praise' . $id, 60)->find();
            self::setPraise($condition['id'], $row);
        }
        if (empty($row)) {
            $result['msg'] = "活动不存在";
            return $result;
        }
        if ($row['status'] == -1) {
            $result['msg'] = "活动不存在!";
            return $result;
        }
        if ($row['status'] == 9) {
            $result['msg'] = "活动不存在!!";
            return $result;
        }
        if ($row['status'] == 1) {
            $result['msg'] = "活动已暂停，请稍后···" . $condition['id'];
            return $result;
        }

        $row['startdate'] = isset($row['startdate']) ? $row['startdate'] : 0;
        $row['enddate'] = isset($row['enddate']) ? $row['enddate'] : 0;
        $result['good_startdate'] = isset($row['startdate']) && $time > $row['startdate'] ? true : false; //
        $result['good_enddate'] = isset($row['enddate']) && $time < $row['enddate'] ? true : false; //

        if (!$result['good_startdate']) {
            $result['msg'] = "活动还没开始";
        } else if (!$result['good_enddate']) {
            $result['msg'] = "活动已经结束";
        } else {
            $result['good'] = true;
        }
        return $result;
    }

    public static function access($praise = -1)
    {
        return 1;//访问控制
    }
}
